<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('User_model');
        $this->load->library('form_validation');
        $this->load->helper('captcha');
    }
    public function Index()
    {


        $this->load->view('admin/login.html');
    }

    public function get_vali_img()
    {
        $this->load->library('Authnum');
        $an = new Authnum();
        $an->ext_num_type='';
        $an->ext_pixel = true; //干扰点
        $an->ext_line  = true; //干扰线
        $an->ext_rand_y= true; //Y轴随机
        $an->green = 238;
        $an->create();
    }

    public function DoLogin()
    {
        $userModel=new User_model();
        $this->load->library('ajaxMsgModel');
        $userModel->user_account=$this->input->post('user_account');
        $userModel->user_pass=md5($this->input->post('user_pass'));
        $valiRes= $this->form_validation->run('lgoin_user_vali');
        $this->load->library('session');
        $vali_img_code=$this->input->post('vali');

        if(!$this->session->has_userdata('an') || empty($this->session->userdata('an')))
        {
            $this->ajaxmsgmodel->Msg='验证码失效，请重新刷新';
            $this->ajaxmsgmodel->Statu='err';
        }
        else if(empty($vali_img_code))
        {
            $this->ajaxmsgmodel->Msg='请填写验证码';
            $this->ajaxmsgmodel->Statu='err';
        }
        else if(strcasecmp($vali_img_code,$this->session->userdata('an'))!=0)
        {
            $this->ajaxmsgmodel->Msg='验证码填写错误';
            $this->ajaxmsgmodel->Statu='err';
        }
        else
        {
            if($valiRes)
            {
                $userModel=$this->User_model->get_login_user($userModel);
                if(count($userModel)==1)
                {
                    $this->session->set_userdata(get_object_vars($userModel[0]));
                   // p($userModel);
                    $this->load->Model('Userlogin_model');
                    $user_login_model=new Userlogin_model();
                    $user_login_model->user_id=$userModel[0]->user_id;
                    $user_login_model->user_login_date=date("Y-m-d H:i:s");
                    $user_login_model->user_login_ip=$this->input->ip_address();

                    $user_login_model->inset_user_login($user_login_model);

                    $this->ajaxmsgmodel->Msg='登陆成功，正在跳转管理首页';
                    $this->ajaxmsgmodel->Statu='ok';
                    $this->ajaxmsgmodel->BackUrl=site_url('admin/sys/config');
                }
                else
                {
                    $this->ajaxmsgmodel->Msg='用户名或者密码错误，请重新输入';
                    $this->ajaxmsgmodel->Statu='err';
                }

            }else{
                $errors=  $this->form_validation->error_array();
                $this->ajaxmsgmodel->Msg=get_validate_errors($errors);
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        echo(json_encode($this->ajaxmsgmodel));

    }

    public function do_exit()
    {
        $this->load->library('session');
        $array_items = array('user_id', 'user_account','user_pass','user_gender','user_name','user_tel','user_phone','user_admin');
        $this->session->unset_userdata($array_items);
        Page_Redirect('admin/login');
    }
}
?>
